/*
LoveWaves Project
Acceliis T4W RFID reader - RFID tag info (tag type and serial number)
Copyright (C) 2006, Eric Sigoillot (e.sigoillot@gmail.com)

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
*/
package lovewaves;

public class TagInfo {
	/** Tag serial number minimal size (in bytes) */
	public static final int MIN_SERIAL_LEN = 5;

	/** Tag type (one byte, see T4W reader manual) */
	public byte[] tagType;
	/** Tag serial number (Unique IDentifier - UID) */
	public byte[] serialNumber;
	
	/** Converts the tag info into a readable form
	* @return The string representing the tag information
	*/
	public String toString() {
		String result = "";
		
		/* Writes the tag type (one byte) */
		if ((tagType != null) && (tagType.length >= 1)) {
			result += String.format("Type: %x\n", tagType[0]);
		}
		
		/* Writes the tag serial number if it is long enough (hexadecimal format) */
		if ((serialNumber != null) && (serialNumber.length >= MIN_SERIAL_LEN)) {
			int i;
			
			result += "ID:   ";
			for (i = 0; i < serialNumber.length; i++) {
				result += String.format("%02X ", serialNumber[i]);
			}
		}
		
		return result;
	}
	
	/**
	* Checks wether the object passed as parameters is equivalent to the current TagInfo instance
	* @param o Object to compare with the current TagInfo instance
	* @return true is the object o is equivalent to the instance
	*/
	public boolean equals(Object o) {
		/* The object must be a TagInfo instance */
		if ((o != null) && (o instanceof TagInfo)) {
			TagInfo t = (TagInfo)o;
			
			/* First, we check global parameters */
			boolean len = (tagType != null) && (t.tagType != null)
							&& (tagType.length == t.tagType.length)
							&& (serialNumber != null) && (t.serialNumber != null)
							&& (serialNumber.length == t.serialNumber.length);
							
			/* If the fields seem to be the same, we check their contents */
			if (len) {
				int i;
				
				/* Tag type comparison */
				for (i = 0; i < tagType.length; i++) {
					if (tagType[i] != t.tagType[i]) return false;
				}
				
				/* Serial number comparison */
				for (i = 0; i < serialNumber.length; i++) {
					if (serialNumber[i] != t.serialNumber[i]) return false;
				}
				
				/* The object is equivalent */
				return true;
			} else {
				/* The object is a tag info, but not the same instance */
				return false;
			}
		} else {
			/* The object is not a TagInfo instance or is null */
			return false;
		}
	}
}